package com;

import java.util.ArrayList;

import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;

public class AddLocationsToGraph {
	
	static SimpleWeightedGraph<Node, DefaultWeightedEdge> newgraph;
	
	/*This method adds the nodes corresponding to the alibi checker to the existing graph of Cambridge.
	For every Location node, it finds the distance from it to every other vertex in the graph,
	chooses the closest vertex and adds the location node to the graph by creating a new vertex
	with an edge to the closest existing vertex with its weight being the distance between them.
	It returns a new graph containing the old vertices plus the alibi checker vertices.*/
	
	public static SimpleWeightedGraph<Node, DefaultWeightedEdge> add(ArrayList<Node> crimeLocations, SimpleWeightedGraph<Node, DefaultWeightedEdge> oldgraph) {
		
		newgraph = oldgraph;
		for (Node futurenode : crimeLocations) {
			Node closestNode = null;
			//initialise shortestDist as "positive infinity" to make first iteration work.
			double shortestDist = Double.POSITIVE_INFINITY;
			for (Node oldnode : newgraph.vertexSet()) {
				double distance = DistBetwNodes.get_distance(oldnode, futurenode);
				if (distance <= shortestDist) {shortestDist = distance;
											   closestNode = oldnode;
											  } else {}
			}
			Node newnode = new Node();
			newnode = futurenode;
			newgraph.addVertex(newnode);
			newgraph.addEdge(closestNode, newnode);
			DefaultWeightedEdge edge = newgraph.getEdge(closestNode, newnode);
			newgraph.setEdgeWeight(edge, shortestDist);
			
		}
		
			
		return newgraph;
	}

}
